
/*
 * 文件名：FtpTool.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2012-4-28
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.chinasoftosg.sicap.scheduling.job;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpTool
{
    
    FtpClient ftpClient = null;
    
    /***
     * 
     * Description: 构造函数<br>
     * @param ip
     * @param username
     * @param password
     * @param port
     * @throws IOException 
     * @see
     */
    public void connect(String ip, String username, String password, int port)
            throws IOException
    {
        ftpClient = new FtpClient();
        ftpClient.openServer(ip, port);
        ftpClient.login(username, password);
        ftpClient.binary();
    }
    
    /***
     * 
     * Description: 构造函数<br>
     * 
     * @param ip
     * @param username
     * @param password
     * @throws IOException 
     * @see
     */
    public void connect(String ip, String username, String password)
            throws IOException
    {
        ftpClient = new FtpClient();
        ftpClient.openServer(ip);
        ftpClient.login(username, password);
        ftpClient.binary();
    }
    
    /** 
     * upload 
     * 上传文件 
     * @throws java.lang.Exception 
     * @return -1 文件不存在 
     *          -2 文件内容为空  
     *          >0 成功上传，返回文件的大小 
     * @param newname 上传后的新文件名 
     * @param filename 上传的文件 
     */
    public long upload(String filename, String newname) throws Exception
    {
        long result = 0;
        TelnetOutputStream os = null;
        FileInputStream is = null;
        try
        {
            java.io.File file_in = new java.io.File(filename);
            if (!file_in.exists())
                return -1;
            if (file_in.length() == 0)
                return -2;
            os = ftpClient.put(newname);
            result = file_in.length();
            is = new FileInputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1)
            {
                os.write(bytes, 0, c);
            }
        }
        finally
        {
            if (is != null)
            {
                is.close();
            }
            if (os != null)
            {
                os.close();
            }
        }
        return result;
    }
    
    /** 
       * upload 
       * @throws java.lang.Exception 
       * @return  
       * @param filename 
       */
    public long upload(String filename) throws Exception
    {
        String newname = "";
        if (filename.indexOf("/") > -1)
        {
            newname = filename.substring(filename.lastIndexOf("/") + 1);
        }
        else
        {
            newname = filename;
        }
        return upload(filename, newname);
    }
    
    /** 
       *  download 
       *  从ftp下载文件到本地 
       * @throws java.lang.Exception 
       * @return  
       * @param newfilename 本地生成的文件名  绝对路径
       * @param filename 服务器上的文件名  绝对路径
       */
    public long download(String filename, String newfilename) throws IOException
    {
        long result = 0;
        TelnetInputStream is = null;
        FileOutputStream os = null;
        try
        {
            is = ftpClient.get(filename);
            java.io.File outfile = new java.io.File(newfilename);
            os = new FileOutputStream(outfile);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1)
            {
                os.write(bytes, 0, c);
                result = result + c;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (is != null)
            {
                is.close();
            }
            if (os != null)
            {
                os.close();
            }
        }
        return result;
    }
    
    /** 
     * 取得某个目录下的所有文件列表 
     * 
     */
    @SuppressWarnings("deprecation")
    public List<String> getFileList(String path)
    {
        List<String> list = new ArrayList<String>();
        try
        {
            DataInputStream dis = new DataInputStream(ftpClient.nameList(path));
            String filename = "";
            while ((filename = dis.readLine()) != null)
            {
                list.add(filename);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return list;
    }
    
    /** 
       * closeServer 
       * 断开与ftp服务器的链接 
       * @throws java.io.IOException 
       */
    public void closeServer()
    {
        try
        {
            if (ftpClient != null)
            {
                ftpClient.closeServer();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
}
